﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace SiteMapService
{
    partial class Service1 : ServiceBase
    {
        #region Attributes

        private string Time = "06:00:00";
        private DateTime RunTime = DateTime.Now;
        private string LogPath = Environment.CurrentDirectory + @"\logs";
        private System.Timers.Timer myTimer = new System.Timers.Timer();

        #endregion

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            try
            {
                Time = ConfigurationManager.AppSettings["Time"];
                RunTime = DateTime.Parse(Time);

                myTimer.Interval = 60 * 1000;
                this.myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
                myTimer.Start();
            }
            catch (Exception ex)
            {
                Tools.outLog(LogPath, ex.Message);
                return;
            }
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
        }

        private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (DateTime.Now >= RunTime)
            {
                SiteMap.CreateSiteMap();
                RunTime.AddDays(1);
            }
        }
    }
}
